

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * String工具类
 *
 * StringUtil gxin 2017年11月30日 上午10:22:40
 * 
 * @version 1.0.0
 *
 */
public class StringUtil {

	/**
	 * 首字母转小写
	 * 
	 * @param s
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();

	}

	/**
	 * 首字母转大写
	 * 
	 * @param s
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	/**
	 * 将string按需要格式化,前面加缩进符,后面加换行符
	 * 
	 * @param tabNum
	 *            缩进量
	 * @param srcString
	 * @return
	 */
	public static String formatSingleLine(int tabNum, String srcString) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < tabNum; i++) {
			sb.append("\t");
		}
		sb.append(srcString);
		sb.append("\n");
		return sb.toString();
	}

	public static String firstToUpperCase(String key) {
		return key.substring(0, 1).toUpperCase(Locale.CHINA) + key.substring(1);
	}

	public static String gapToCamel(String src) {
		StringBuilder sb = new StringBuilder();
		for (String s : src.trim().split(" ")) {
			sb.append(firstToUpperCase(s));
		}
		return sb.toString();
	}

	/**
	 * 驼峰转下划线命名
	 * 
	 * @param src
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String camelTo_(String src) {
		StringBuilder sb = new StringBuilder();
		StringBuilder sbWord = new StringBuilder();
		char[] chars = src.trim().toCharArray();
		for (int i = 0; i < chars.length; i++) {
			char c = chars[i];
			if (c >= 'A' && c <= 'Z') {
				// 一旦遇到大写单词，保存之前已有字符组成的单词
				if (sbWord.length() > 0) {
					if (sb.length() > 0) {
						sb.append("_");
					}
					sb.append(sbWord.toString());
				}
				sbWord = new StringBuilder();
			}
			sbWord.append(c);
		}

		if (sbWord.length() > 0) {
			if (sb.length() > 0) {
				sb.append("_");
			}
			sb.append(sbWord.toString());
		}

		return sb.toString();
	}

	/**
	 * 是否包含中文
	 * 
	 * @param s
	 * @return boolean
	 * @exception @since
	 *                1.0.0
	 */
	public static boolean hasChinese(String s) {
		String regexChinese = "[\u4e00-\u9fa5]+";
		Pattern patternChinese = Pattern.compile(regexChinese);
		return patternChinese.matcher(s).find();
	}

	/**
	 * 是否为空
	 * 
	 * @param s
	 * @return boolean
	 * @exception @since
	 *                1.0.0
	 */
	public static boolean isEmpty(String s) {
		return s == null || s.length() == 0;
	}

	
	/**
	 * 去掉字符串前面的默认空格
	 * 
	 * @param s
	 * @return String
	 * @exception @since
	 *                1.0.0
	 */
	public static String trimStr(String s) {
		char[] charArray = s.toCharArray();
		List<Character> list = new ArrayList<Character>();
		for (Character c : charArray) {
			Character charValue = c.charValue();
			if ((int) charValue != 8234) {
				list.add(c);
			}
		}
		char[] newChar = new char[list.size()];
		for (int i = 0; i < list.size(); i++) {
			newChar[i] = list.get(i);
		}
		return String.valueOf(newChar);
	}

	/**
	 * 判断字符串是否是数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		if (!"".equals(str) && str != null) {
			for (int i = 0; i < str.length(); i++) {
				if (!Character.isDigit(str.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 格式化时间
	 * @param value
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String formatDate(String value) {
		try {
			Date date = null;
			if (value == null) {
				date = new Date();
			} else {
				date = new Date(value);
			}
			SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			value = dFormat.format(date);
		} catch (Exception e) {
		}
		return value;
	}

}
